// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BitmapLocker.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the BitmapLocker type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TaskbarButtons
{
  #region Namespace Imports

  using System;
  using System.Drawing;
  using System.Drawing.Imaging;

  using Microsoft.VisualStudio.PlatformUI;

  #endregion


  /// <summary>
  /// Describes and manipulates the bitmap locker.
  /// </summary>
  public class BitmapLocker : DisposableObject
  {
    #region Constants and Fields

    private readonly Bitmap _bitmap;
    private readonly BitmapData _bitmapData;

    #endregion


    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="BitmapLocker"/> class.
    /// </summary>
    /// <param name="bitmap">
    /// The bitmap.
    /// </param>
    public BitmapLocker(Bitmap bitmap)
      : this(bitmap, ImageLockMode.ReadOnly)
    {
    }


    /// <summary>
    /// Initializes a new instance of the <see cref="BitmapLocker"/> class.
    /// </summary>
    /// <param name="bitmap">
    /// The bitmap.
    /// </param>
    /// <param name="mode">
    /// The lock mode.
    /// </param>
    public BitmapLocker(Bitmap bitmap, ImageLockMode mode)
    {
      if (bitmap == null)
      {
        throw new ArgumentNullException("bitmap");
      }

      _bitmap = bitmap;
      var rect = new Rectangle(0, 0, _bitmap.Width, _bitmap.Height);
      _bitmapData = bitmap.LockBits(rect, mode, _bitmap.PixelFormat);
    }


    /// <summary>
    /// Initializes a new instance of the <see cref="BitmapLocker"/> class.
    /// </summary>
    /// <param name="bitmap">
    /// The bitmap.
    /// </param>
    /// <param name="mode">
    /// The lock mode.
    /// </param>
    /// <param name="rectangle">
    /// The rectangle.
    /// </param>
    public BitmapLocker(Bitmap bitmap, ImageLockMode mode, Rectangle rectangle)
    {
      if (bitmap == null)
      {
        throw new ArgumentNullException("bitmap");
      }

      _bitmap = bitmap;
      _bitmapData = bitmap.LockBits(rectangle, mode, _bitmap.PixelFormat);
    }

    #endregion


    #region Properties

    /// <summary>
    /// Gets the bitmap.
    /// </summary>
    /// <value>
    /// The bitmap.
    /// </value>
    public Bitmap Bitmap
    {
      get
      {
        return _bitmap;
      }
    }

    /// <summary>
    /// Gets the bitmap data.
    /// </summary>
    /// <value>
    /// The bitmap data.
    /// </value>
    public BitmapData BitmapData
    {
      get
      {
        return _bitmapData;
      }
    }

    #endregion


    #region Methods

    /// <summary>
    /// Disposes the managed resources.
    /// </summary>
    protected override void DisposeManagedResources()
    {
      _bitmap.UnlockBits(_bitmapData);
      base.DisposeManagedResources();
    }

    #endregion
  }
}